package es.ats.codington.festival.services;

import java.sql.SQLException;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import es.ats.codington.festival.daos.DAOManager;

import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.interfaces.daos.IVisitorDAO;
import es.ats.codington.festival.interfaces.services.IVisitorService;

/**
 * The Class VisitorServiceImpl.
 */
public class VisitorServiceImpl implements IVisitorService{
	
	/** The visitor dao. */
	private IVisitorDAO visitorDAO;
	
	/**
	 * Instantiates a new visitor service impl.
	 */
	public VisitorServiceImpl(){
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");  
		DAOManager daoManager = (DAOManager) ctx.getBean("daoManager");
		this.visitorDAO = daoManager.getVisitorDAO();
	}

	/**
	 * Login visitor.
	 *
	 * @param visitor the visitor
	 * @return the visitor
	 * @throws SQLException the SQL exception
	 * @throws ClassNotFoundException the class not found exception
	 * @see es.ats.codington.festival.interfaces.services.IVisitorService#loginVisitor(java.lang.String, java.lang.String)
	 */
	public Visitor login(Visitor visitor) throws SQLException, ClassNotFoundException{			
		return visitorDAO.loginDAO(visitor);	
	}
	
	
	/**
	 * Search visitor by id.
	 *
	 * @param idVisitor the id visitor
	 * @return the visitor
	 * @throws Exception the exception
	 */
	public Visitor searchVisitorById (int idVisitor) throws Exception{			
		return visitorDAO.searchVisitorDAOById(idVisitor);	
	}
	
	/**
	 * Update visitor.
	 *
	 * @param visitor the visitor
	 * @return the int
	 * @throws Exception the exception
	 */
	public int updateVisitor (Visitor visitor) throws Exception{
		return visitorDAO.updateVisitorDAO(visitor);
	}
	
	/**
	 * Register new visitor.
	 *
	 * @param visitor the visitor
	 * @return true if user could register successfully
	 * @throws SQLException the SQL exception
	 * @throws ClassNotFoundException the class not found exception
	 */
	public boolean registerNewVisitor(Visitor visitor) throws SQLException, ClassNotFoundException{	
		return  visitorDAO.registerVisitorDAO(visitor);
	}
	
	public boolean deleteVisitor(int idVisitor) throws Exception{	
		return  visitorDAO.deleteVisitorDAO(idVisitor);
	}
	
	/**
	 * Update visitor pass.
	 *
	 * @param visitor the visitor
	 * @return true, if successful
	 * @throws SQLException the SQL exception
	 * @throws ClassNotFoundException the class not found exception
	 * @see es.ats.codington.festival.interfaces.services.IVisitorService#updateVisitorPass(es.ats.codington.festival.domain.Visitor)
	 */
	public boolean updateVisitorPass(Visitor visitor)throws SQLException, ClassNotFoundException{	
		return  visitorDAO.updateVisitorPassDAO(visitor);
	}
	
	/**
	 * List all visitors.
	 *
	 * @return the list
	 * @throws SQLException the SQL exception
	 * @throws ClassNotFoundException the class not found exception
	 * @see es.ats.codington.festival.interfaces.services.IVisitorService#listAllVisitors()
	 */
	public List<Visitor> listAllVisitors(int idAdmin)throws SQLException, ClassNotFoundException{		
		return  visitorDAO.searchAllVisitorDAO(idAdmin);
	}
}
